package com.lehe.wxpay;

import java.util.HashMap;
import java.util.Map;

/**
 * 类的相关描述
 *
 * @author 王丹
 * @since 2017-09-22
 */
public class WXPayTest {

    public static void main(String[] args) throws Exception {
        //getsignkey();
        //unifiedOrder();
        orderQuery();
    }

    public static void getsignkey() throws Exception{
        WXPayConfigImpl config = new WXPayConfigImpl();
        WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5, true);
        Map<String, String> resp = wxpay.getsignkey();
        if(WXPayUtil.isSuccess(resp)){
            System.out.println(resp.get("sandbox_signkey"));
        }else {
            System.out.println(resp.get("return_msg"));
        }
    }

    /**
     * 统一下单
     * @throws Exception
     */
    public static void unifiedOrder() throws Exception {
        WXPayConfigImpl config = new WXPayConfigImpl();
        //WXPay wxpay = new WXPay(config);
        WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5, true);
        Map<String, String> data = new HashMap<String, String>();
        data.put("body", "腾讯充值中心-QQ会员充值");
        data.put("out_trade_no", "2016090910595900000012");
        data.put("device_info", "");
        data.put("fee_type", "CNY");
        data.put("total_fee", "101");
        data.put("spbill_create_ip", "123.12.12.123");
        data.put("notify_url", "http://www.example.com/wxpay/notify");
        data.put("trade_type", "JSAPI");  // 此处指定为扫码支付
        data.put("product_id", "12");

        try {
            Map<String, String> resp = wxpay.unifiedOrder(data);
            if(WXPayUtil.isSuccess(resp)){
                System.out.println(resp.get("prepay_id"));
            }else {
                System.out.println(resp.get("return_msg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 订单查询
     * @throws Exception
     */
    public static void orderQuery() throws Exception {
        WXPayConfigImpl config = new WXPayConfigImpl();
        WXPay wxpay = new WXPay(config, WXPayConstants.SignType.MD5, true);
        Map<String, String> data = new HashMap<String, String>();
        data.put("out_trade_no", "2016090910595900000012");

        try {
            Map<String, String> resp = wxpay.orderQuery(data);
            System.out.println(resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 收到支付结果通知时，需要验证签名，可以这样做
     * @throws Exception
     */
    private static void payResultNotify() throws Exception{
        String notifyData = "...."; // 支付结果通知的xml格式数据

        WXPayConfigImpl config = new WXPayConfigImpl();
        WXPay wxpay = new WXPay(config);

        Map<String, String> notifyMap = WXPayUtil.xmlToMap(notifyData);  // 转换成map

        if (wxpay.isPayResultNotifySignatureValid(notifyMap)) {
            // 签名正确
            // 进行处理。
            // 注意特殊情况：订单已经退款，但收到了支付结果成功的通知，不应把商户侧订单状态从退款改成支付成功
        }
        else {
            // 签名错误，如果数据里没有sign字段，也认为是签名错误
        }

    }

    private static void other() throws Exception{
        //HTTPS请求默认使用MD5算法签名，若需要使用HMAC-SHA256
        WXPayConfigImpl config = new WXPayConfigImpl();
        WXPay wxpay = new WXPay(config, WXPayConstants.SignType.HMACSHA256);
        // ......

        //若需要使用sandbox环境
        WXPayConfigImpl config1 = new WXPayConfigImpl();
        WXPay wxpay1 = new WXPay(config, WXPayConstants.SignType.MD5, true);
        // ......

    }

}
